package com.gexiaobao.pigeon.ui.fragment.mall

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.viewpager.widget.ViewPager
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.databinding.FragmentBigImageBinding
import com.gexiaobao.pigeon.ui.adapter.PhotoPagerAdapter
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat


/**
 * @Author      : hxw
 * @Date        : 2024/2/20 13:53
 * @Describe    :
 */
class FragmentBigImage : BaseFragment<MallViewModel, FragmentBigImageBinding>() {
    @SuppressLint("SetTextI18n")
    override fun initView(savedInstanceState: Bundle?) {
        /** 接收图片数据及位置 */
        val imageData = requireArguments().getStringArrayList("imageList")
        val position = requireArguments().getInt("clickPosition", 0)

        /** 添加适配器 */
        val viewPagerAdapter = PhotoPagerAdapter(childFragmentManager, imageData)
        mDatabind.viewpager.adapter = viewPagerAdapter
        mDatabind.viewpager.currentItem = position //设置选中图片位置
        if (imageData != null) {
            mDatabind.tvNum.text = (position + 1).toString() + "/" + imageData.size
        }

        mDatabind.viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            @SuppressLint("SetTextI18n")
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                if (imageData != null) {
                    mDatabind.tvNum.text = (position + 1).toString() + "/" + imageData.size
                }
            }

            override fun onPageSelected(position: Int) {}
            override fun onPageScrollStateChanged(state: Int) {}
        })
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.ivCloseBigImage) {
            when (it) {
                mDatabind.ivCloseBigImage -> {
                    nav().navigateUp()
                }
            }
        }
    }
}